//
//  PPNotificationSettingViewController.m
//  pickpic
//
//  Created by Jinxiao on 8/27/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPNotificationSettingViewController.h"

@interface PPNotificationSettingViewController ()
@property (weak, nonatomic) IBOutlet UIView *line1;
@property (weak, nonatomic) IBOutlet UIView *line2;
@property (weak, nonatomic) IBOutlet UIView *line3;
@property (weak, nonatomic) IBOutlet UIView *line4;
@property (weak, nonatomic) IBOutlet UIView *line5;
@property (weak, nonatomic) IBOutlet UIView *line6;
@property (weak, nonatomic) IBOutlet UIView *line7;
@property (weak, nonatomic) IBOutlet UIView *line8;
@property (weak, nonatomic) IBOutlet UIView *line9;
@property (weak, nonatomic) IBOutlet UIView *line10;
@property (weak, nonatomic) IBOutlet UIView *line11;

@property (weak, nonatomic) IBOutlet UIView *line12;
@property (weak, nonatomic) IBOutlet UIView *line13;
@property (weak, nonatomic) IBOutlet UIView *line14;
@property (weak, nonatomic) IBOutlet UIView *line15;
@property (weak, nonatomic) IBOutlet UIView *line16;
@property (weak, nonatomic) IBOutlet UISwitch *settingSwitch;
@end
@implementation PPNotificationSettingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [MobClick event:@"163"];

    [_line1 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line2 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line3 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line4 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line5 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line6 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line7 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line8 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line9 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line10 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line11 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line12 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line13 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line14 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line15 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    [_line16 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];

    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    if ([profiles.msgCfgAll integerValue] == 1)
    {
        _settingSwitch.on = YES;
    }
    else
    {
        _settingSwitch.on = NO;
    }
}

- (IBAction)switchValueDidChanged:(UISwitch *)sender {

    __weak typeof(self)weakSelf = self;
    if (sender.on)
    {
        //全部打开
        [PPNetworkService updateMessageConfigWithAll:@1 like:nil comment:nil fan:nil pickpick:nil callback:^(id data, NSError *error) {
            if (data)
            {
                [PPAccountProfiles performInBlock:^(NSManagedObjectContext *context) {
                    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
                    profiles.msgCfgAll = @1;
                    profiles.msgCfgLike = @1;
                    profiles.msgCfgComment = @1;
                    profiles.msgCfgFan = @1;
                    profiles.msgCfgPickpic = @1;

                    [context saveContext];
                }];
                [weakSelf.tableView reloadData];
            }
            else
            {
                sender.on = NO;
                [weakSelf.navigationController.view toastMessage:error.networkPrompt duration:1.f];
            }
        }];
    }
    else
    {
        [MobClick event:@"166"];
        [PPNetworkService updateMessageConfigWithAll:@0 like:@4 comment:@4 fan:@2 pickpick:@2 callback:^(id data, NSError *error) {
            if (data)
            {
                [PPAccountProfiles performInBlock:^(NSManagedObjectContext *context) {
                    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
                    profiles.msgCfgAll = @0;
                    profiles.msgCfgLike = @4;
                    profiles.msgCfgComment = @4;
                    profiles.msgCfgFan = @2;
                    profiles.msgCfgPickpic = @2;

                    [context saveContext];
                }];
                [weakSelf.tableView reloadData];
            }
            else
            {
                sender.on = YES;
                [weakSelf.navigationController.view toastMessage:error.networkPrompt duration:1.f];
            }
        }];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_settingSwitch.on)
    {
        return 1;
    }
    return 5;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    if (indexPath.section == 0)
    {

    }
    else if (indexPath.section == 1)
    {
        //赞  赞消息设置：1:所有人;2:朋友;4:关闭;
        NSNumber *likeSetting = profiles.msgCfgLike;
        UIImageView *indicatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"radio"]];
        cell.accessoryView = ((indexPath.row == 0 && [likeSetting integerValue] == 1) || (indexPath.row == 1 && [likeSetting integerValue] == 2) || (indexPath.row == 2 && [likeSetting integerValue] == 4)) ? indicatorView : nil;
    }
    else if (indexPath.section == 2)
    {
        //评论  评论消息设置：1:所有人;2:朋友;4:关闭;
        NSNumber *commentSetting = profiles.msgCfgComment;
        UIImageView *indicatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"radio"]];
        cell.accessoryView = ((indexPath.row == 0 && [commentSetting integerValue] == 1) || (indexPath.row == 1 && [commentSetting integerValue] == 2) || (indexPath.row == 2 && [commentSetting integerValue] == 4)) ? indicatorView : nil;
    }
    else if (indexPath.section == 3)
    {
        //关注  关注消息设置：1打开，2关闭
        NSNumber *fanSetting = profiles.msgCfgFan;
        UIImageView *indicatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"radio"]];
        cell.accessoryView = ((indexPath.row == 0 && [fanSetting integerValue] == 1) || (indexPath.row == 1 && [fanSetting integerValue] == 2)) ? indicatorView : nil;
    }
    else if (indexPath.section == 4)
    {
        //pickpic消息 系统消息设置：1打开，2关闭
        NSNumber *pickpicSetting = profiles.msgCfgPickpic;
        UIImageView *indicatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"radio"]];
        cell.accessoryView = ((indexPath.row == 0 && [pickpicSetting integerValue] == 1) || (indexPath.row == 1 && [pickpicSetting integerValue] == 2)) ? indicatorView : nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    __weak typeof(self)weakSelf = self;
    if (indexPath.section == 0) {
        return;
    }

    NSNumber *likeSetting = nil;
    NSNumber *commentSetting = nil;
    NSNumber *fanSetting = nil;
    NSNumber *pickpicSetting = nil;
    if (indexPath.section == 1)
    {
        //赞
        likeSetting = indexPath.row == 0 ? @1 : (indexPath.row == 1 ? @2 : @4);
        if ([likeSetting integerValue] == 2)
        {
            [MobClick event:@"167"];
        }
        else if ([likeSetting integerValue] == 4)
        {
            [MobClick event:@"168"];
        }
    }
    else if (indexPath.section == 2)
    {
        //评论
        commentSetting = indexPath.row == 0 ? @1 : (indexPath.row == 1 ? @2 : @4);
        if ([commentSetting integerValue] == 2)
        {
            [MobClick event:@"169"];
        }
        else if ([commentSetting integerValue] == 4)
        {
            [MobClick event:@"170"];
        }
    }
    else if (indexPath.section == 3)
    {
        //关注
        fanSetting = indexPath.row == 0 ? @1 : @2;

        if ([fanSetting integerValue] == 2)
        {
            [MobClick event:@"171"];
        }
    }
    else if (indexPath.section == 4)
    {
        //pickpic消息
        pickpicSetting = indexPath.row == 0 ? @1 : @2;

        if ([pickpicSetting integerValue] == 2)
        {
            [MobClick event:@"172"];
        }
    }

    [PPNetworkService updateMessageConfigWithAll:@0 like:likeSetting comment:commentSetting fan:fanSetting pickpick:pickpicSetting callback:^(id data, NSError *error) {
        if (data)
        {
            [PPAccountProfiles performInBlock:^(NSManagedObjectContext *context) {
                PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;

                if (likeSetting)
                {
                    profiles.msgCfgLike = likeSetting;
                }
                else if (commentSetting)
                {
                    profiles.msgCfgComment = commentSetting;
                }
                else if (fanSetting)
                {
                    profiles.msgCfgFan = fanSetting;
                }
                else if (pickpicSetting)
                {
                    profiles.msgCfgPickpic = pickpicSetting;
                }

                [context saveContext];
            }];

            [weakSelf.tableView reloadData];
        }
        else
        {
            [weakSelf.navigationController.view toastMessage:error.networkPrompt duration:1.f];
        }
    }];

}

@end
